package com.singfan.app.util;

import com.singfan.app.controller.model.response.partial.AppointmentPartial;
import com.singfan.core.dao.database.model.BarberAppointment;
import com.singfan.core.dao.database.model.constant.AppointmentType;

import java.util.*;

/**
 * Author: hansonghe
 * Date: 2015-09-01 10:23
 * Desc:
 */
public class AppointmentUtil {
    public static List<Integer> createOneDayAppointmentList(){
        List<Integer> list = new ArrayList<>();

        list.add(900);
        list.add(930);
        list.add(1000);
        list.add(1030);

        list.add(1100);
        list.add(1130);

        list.add(1200);
        list.add(1230);

        list.add(1300);
        list.add(1330);

        list.add(1400);
        list.add(1430);

        list.add(1500);
        list.add(1530);

        list.add(1600);
        list.add(1630);

        list.add(1700);
        list.add(1730);

        list.add(1800);
        list.add(1830);

        list.add(1900);
        list.add(1930);

        list.add(2000);
        list.add(2030);

        list.add(2100);
        list.add(2130);

        list.add(2200);
        list.add(2230);

        return list;
    }

    public static List<Integer> createPeriodAppointmentList(int period){
        List<Integer> list = new ArrayList<>();
        if (period == 1){
            list.add(900);
            list.add(930);
            list.add(1000);
            list.add(1030);

            list.add(1100);
            list.add(1130);
        }
        else if (period == 2){
            list.add(1200);
            list.add(1230);

            list.add(1300);
            list.add(1330);

            list.add(1400);
            list.add(1430);

            list.add(1500);
            list.add(1530);

            list.add(1600);
            list.add(1630);

            list.add(1700);
            list.add(1730);
        }
        else if (period == 3){
            list.add(1800);
            list.add(1830);

            list.add(1900);
            list.add(1930);

            list.add(2000);
            list.add(2030);

            list.add(2100);
            list.add(2130);

            list.add(2200);
            list.add(2230);
        }

        return list;
    }

    public static List<AppointmentPartial> getAppointmentPartialList(int dayTime, List<BarberAppointment> appointmentList) {
        int todayTime = AppointmentUtil.getTodayTime();
        Set<Integer> bookedSet = new HashSet<>();
        Set<Integer> noBookSet = new HashSet<>();
        for (BarberAppointment barberAppointment : appointmentList){
            if (barberAppointment.getCustomerId() > 0){
                bookedSet.add(barberAppointment.getAppointmentClock());
            }
            else {
                noBookSet.add(barberAppointment.getAppointmentClock());
            }
        }
        int nowClock = AppointmentUtil.getNowClock();
        List<AppointmentPartial> appointmentPartialList = new ArrayList<>();
        List<Integer> keySet = AppointmentUtil.createOneDayAppointmentList();
        for (Integer clock : keySet) {
            AppointmentPartial appointmentPartial = new AppointmentPartial();
            appointmentPartial.setClock(clock);
            if (dayTime == todayTime){
                if (clock > nowClock){
                    appointmentPartial.setStatus(AppointmentType.BOOKING);
                }
                else {
                    appointmentPartial.setStatus(AppointmentType.EXPIRED);
                }
            }
            else if (dayTime > todayTime){
                appointmentPartial.setStatus(AppointmentType.BOOKING);
            }
            else {
                appointmentPartial.setStatus(AppointmentType.EXPIRED);
            }


            if (bookedSet.contains(clock)){
                appointmentPartial.setStatus(AppointmentType.BOOKED);
            }
            else if (noBookSet.contains(clock)){
                appointmentPartial.setStatus(AppointmentType.NO_BOOK);
            }

            appointmentPartialList.add(appointmentPartial);
        }
        return appointmentPartialList;
    }

    public static int getTodayTime(){
        Date now = new Date();
        int year = now.getYear() + 1900;
        int month = now.getMonth() + 1;
        int day = now.getDate();
        String monthText = fill0(month);
        String dayText = fill0(day);

        return Integer.parseInt(String.format("%1$d%2$s%3$s", now.getYear() + 1900, monthText, dayText));
    }


    public static int getMonthAndDay(int appointmentDay){
        String appointmentDayStr = String.valueOf(appointmentDay);
        String md = appointmentDayStr.substring(4);
        return Integer.parseInt(md);
    }

    public static int getNowClock(){
        Date now = new Date();
        int hour = now.getHours();
        int min = now.getMinutes();
        return Integer.parseInt(fill0(hour) + "" + fill0(min));
    }

    private static String fill0(int value){
        String result;

        if (value < 10){
            result = "0" + value;
        }
        else{
            result = String.valueOf(value);
        }

        return result;
    }
}
